package com.mostromas.ess.system.controller;

import com.mostromas.ess.entities.SysMenu;
import com.mostromas.ess.system.req.SysMenuREQ;
import com.mostromas.ess.system.service.ISysMenuService;
import com.mostromas.ess.util.base.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 菜单信息表-前端控制器
 */
@Api(value = "菜单信息管理接口",description = "菜单信息管理接口，提供菜单信息的增、删、改、查")
@RestController //所有方法都会返回json字符串进行相应
@RequestMapping("/menu")
public class SysMenuController {

    @Autowired
    private ISysMenuService sysMenuService;

    /**
     * 根据菜单名称查询菜单列表
     * @param req
     * @return
     */
    @ApiOperation("根据菜单名称查询菜单列表接口")
    @PreAuthorize(("hasAuthority('menu:search')"))
    @PostMapping("/search") //请求路径：/system/menu/search
    public Result search(@RequestBody SysMenuREQ req){
        return sysMenuService.queryList(req);
    }

    @ApiOperation("查询菜单详情接口")
    @ApiImplicitParam(name = "id",value = "菜单ID",required = true)
    @PreAuthorize(("hasAuthority('menu:search')"))
    @GetMapping("/{id}") //请求路径：/system/menu/{id}
    public Result view(@PathVariable("id") String id){
        SysMenu sysMenu = sysMenuService.getById(id);
        return Result.ok(sysMenu);
    }

    @ApiOperation("修改菜单信息接口")
    @PreAuthorize(("hasAuthority('menu:edit')"))
    @PutMapping //put方式调用接口：/system/menu
    public Result update(@RequestBody SysMenu sysMenu){
        sysMenu.setUpdateDate(new Date());
        sysMenuService.updateById(sysMenu);
        return Result.ok();
    }

    @ApiOperation("新增菜单信息接口")
    @PreAuthorize(("hasAuthority('menu:add')"))
    @PostMapping //post方式调用接口：/system/menu
    public Result save(@RequestBody SysMenu sysMenu){
        sysMenuService.save(sysMenu);
        return Result.ok();
    }

    @ApiOperation("通过菜单ID删除菜单信息接口")
    @PreAuthorize(("hasAuthority('menu:delete')"))
    @ApiImplicitParam(name = "id",value = "菜单ID",required = true)
    @DeleteMapping("/{id}") //delete方式调用接口：/system/menu/{id}
    public Result delete(@PathVariable("id") String id){
        sysMenuService.deleteById(id);
        return Result.ok();
    }

    @ApiOperation("通过用户ID查询拥有的权限菜单树")
    @PreAuthorize(("hasAuthority('menu:search')"))
    @ApiImplicitParam(name="userId", value="用户ID", required=true)
    @GetMapping("/user/{userId}") // /menu/user/1
    public Result findUserMenuTree(@PathVariable("userId") String userId) {
        return sysMenuService.findUserMenuTree(userId);
    }
}
